package com.ncloud.resman.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.ncloud.resman.entity.Brand;

@Mapper
public interface BrandMapper {

	@Insert("insert into res_base_brand(brand_id,eqpt_type,brand_name,state,create_user,create_date,update_user,update_date,remarks)"
			+ " values(#{brandId},#{eqptType},#{brandName},#{state},#{createUser},#{createDate},#{updateUser},#{updateDate},#{remarks})")
	int insertBrand(Brand brand);

	@Update("update res_base_brand set brand_name = #{brandName},update_date=#{updateDate},update_user =#{updateUser},"
			+ " remarks=#{remarks} where brand_id = #{brandId} and state = '1'")
	int updateBrand(Brand brand);

	@Delete("update res_base_brand set state=#{state},update_date=#{updateDate},update_user =#{updateUser}"
			+ " where brand_id=#{brandId} and state = '1'")
	int deleteBrand(Brand brand);

	@Select("select eqpt_type, brand_id, brand_name,remarks from res_base_brand"
			+ " where eqpt_type = ifnull(#{eqptType},eqpt_type) and state ='1'")
	@Results({ @Result(column = "brand_id", property = "brandId"), @Result(column = "eqpt_type", property = "eqptType"),
			@Result(column = "brand_name", property = "brandName"), @Result(column = "remarks", property = "remarks") })
	List<Brand> findBrandsByType(String eqptType);
	
	@Select("select eqpt_type eqptType, brand_id brandId, brand_name brandName,remarks remarks,"
			+ " (select dict_value from sys_dictionary t1 where dictionary_id = 'eqpt_unit_type' and t1.dict_key=a.eqpt_type) as eqptTypeName"
			+ " from res_base_brand a where eqpt_type = ifnull(#{eqptType},eqpt_type) and state ='1'")
	List<Map<String, Object>> findBrandListByType(String eqptType);

	@Select("select eqpt_type, brand_id, brand_name,remarks from res_base_brand"
			+ " where brand_id = #{brandId}")
	@Results({ @Result(column = "brand_id", property = "brandId"), @Result(column = "eqpt_type", property = "eqptType"),
			@Result(column = "brand_name", property = "brandName"), @Result(column = "remarks", property = "remarks") })
	Brand getByBrandId(Long brandId);

	@Select("select eqpt_type, brand_id, brand_name,remarks from res_base_brand"
			+ " where brand_name = #{brandName} and eqpt_type=#{eqptType} and state ='1'")
	@Results({ @Result(column = "brand_id", property = "brandId"), @Result(column = "eqpt_type", property = "eqptType"),
		@Result(column = "brand_name", property = "brandName"), @Result(column = "remarks", property = "remarks") })
	List<Brand> getBrandByEqptTypeAndBrandName(@Param("eqptType")String eqptType, @Param("brandName")String brandName);
}
